package com.xcity.game.employee.config.impl;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.employee.EmployeeFilter;
import com.xcity.game.employee.config.AbstractConfigure;
import com.xcity.game.employee.config.Configure;
import com.xcity.game.employee.response.EmployeeLevelUpRequired;
import com.xcity.game.employee.template.EmployeeStarUpTemplate;
import com.xcity.game.employee.template.EmployeeTemplate;
import com.xcity.game.item.ItemInfo;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;

/**
 * 员工升星配置
 * @author yang.li
 *
 */
public class EmployeeStarLevelUpConfigure extends AbstractConfigure {
	
	private TIntObjectMap<List<EmployeeStarUpTemplate>> templates; // key: employee template id
	
	public EmployeeStarLevelUpConfigure(Configure parent) {
		super(parent);
	}
	
	@Override
	public EmployeeStarLevelUpConfigure configure() {
		List<EmployeeStarUpTemplate> list = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(EmployeeStarUpTemplate.class);
		TIntObjectMap<List<EmployeeStarUpTemplate>> templates = new TIntObjectHashMap<>();
		for (int i = 0; i < list.size(); i++) {
			EmployeeStarUpTemplate template = list.get(i);
			List<EmployeeStarUpTemplate> group = templates.get(template.getId());
			if (group == null) {
				group = new ArrayList<EmployeeStarUpTemplate>(10);
				templates.put(template.getId(), group);
			}
			group.add(template);
		}
		this.templates = templates;
		return this;
	}

	public EmployeeStarUpTemplate getNextLevelTemplate(Employee employee) {
		int index = employee.getStarLvl();
		List<EmployeeStarUpTemplate> list = templates.get(employee.getTemplate().getId());
		return list != null && index >= 0 && index < list.size() ? list.get(index) : null;
	}
	
	public int getMaxLevel(Employee employee) {
		List<EmployeeStarUpTemplate> list = templates.get(employee.getTemplate().getId());
		return list != null ? list.size() : 0;
	}
	
	public List<EmployeeStarUpTemplate> getTemplates(int templateId) {
		return templates.get(templateId);
	}
	
	// 主卡
	public ItemInfo getWrappedMainCard(Player player, Employee employee) {
		return getWrappedMainCard(player, employee, getNextLevelTemplate(employee));
	}
	
	public static ItemInfo getWrappedMainCard(Player player, Employee employee, EmployeeStarUpTemplate template) {
		return getWrappedCard(player, employee, employee.getTemplate(), template.getMainCardCount());
	}

	// 副卡
	public ItemInfo getWrappedViceCard(Player player, Employee employee) {
		return getWrappedViceCard(player, employee, getNextLevelTemplate(employee));
	}
	
	public static ItemInfo getWrappedViceCard(Player player, Employee employee, EmployeeStarUpTemplate template) {
		if (template.getViceCardId() == 0) {
			return null;
		}
		EmployeeTemplate et = App.getApp().getServiceManager().get(TemplateService.class).get(EmployeeTemplate.class, template.getViceCardId());
		int count = template.getViceCardCount();
		return getWrappedCard(player, employee, et, count);
	}
	
	private static ItemInfo getWrappedCard(final Player player, final Employee employee, final EmployeeTemplate cardTemplate, final int cardCount) {
		if (cardTemplate == null || cardCount <= 0) {
			return null;
		}
		int cardId = cardTemplate.getId();
		String name = cardTemplate.getName();
		String icon = cardTemplate.getIcon();
		String desc = "";
		EmployeeFilter filter = new EmployeeFilter() {
			@Override
			public boolean doFilter(Employee e) {
				return e.getId() != employee.getId() && !e.isTrained();
			}
		};
		int ownedCount = player.getEmployees().size(cardId, filter);
		ItemInfo req = new EmployeeLevelUpRequired(cardId, cardCount, name, icon, desc, ownedCount);
		return req;
	}
	
}
